/*
 * 文件名： MainActivity.java
 * 描    述：
 * 时    间：2015-3-25 下午5:47:21
 * 版    本：V1.0
 */
package com.miguo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;

import com.miguo.activity.base.BaseActivity;
import com.miguo.activity.fragment.CategoryFragment;
import com.miguo.activity.fragment.UserCenterFragment;
import com.miguo.app.AppConfig;
import com.miguo.component.FooterTabView;
import com.miguo.component.FooterTabView.OnTabSelectedListener;
import com.miguo.framework.utils.StatusBarUtil;
import com.miguo.framework.utils.Toast;
import com.miguo.plugins.Statistics;
import com.miguo.plugins.UmengFeedback;
import com.miguo.plugins.UmengUp;
import com.miguo.ui.R;
import com.miguo.utils.StringUtils;
import com.mrper.ui.main.FragmentBrandCategory;
import com.mrper.ui.main.FragmentDoubleNine;
import com.mrper.ui.main.FragmentHome;

public class MainActivity extends BaseActivity implements OnTabSelectedListener {

    private Long exitTime = 1L;

    private FragmentHome fragmentHome;
//    private IndexFragment mIndexFragment;
    private CategoryFragment mCategoryFragment;
//    private JiuFragment mJiuFragment;
    private FragmentDoubleNine fragmentDoubleNine;
    private FragmentBrandCategory fragmentBrandCategory;
//    private ScoreShopFragment mScoreShopFragment;
    private UserCenterFragment mUserCenterFragment;

    // 底部导航
    private FooterTabView TabWidget;

    private FragmentManager mFragmentManager;
    private FragmentTransaction mTransaction;

    private int position = AppConfig.TAB_DEFAULT_DISPLAY;

    // 更新指示点
    private boolean TabVisible = AppConfig.TAB_VISIBLE;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initExtra();
        initView();
        /* 友盟更新与反馈信息 */
        UmengUp.update(mContext);
        UmengFeedback.sync(mContext);
    }

    private void initExtra() {
        //--------------------------  END   -----------------------------------
        Intent in = getIntent();
        if (in.hasExtra("position")) {
            position = StringUtils.StrToInt(in.getStringExtra("position"));
        }
        //兼容旧版本
        if (in.hasExtra("Display")) {
            position = StringUtils.StrToInt(in.getStringExtra("Display"));
        }
    }

    private void initView() {
        TabWidget = (FooterTabView) findViewById(R.id.tab_widget);
        mFragmentManager = getSupportFragmentManager();
        TabWidget.setOnTabSelectedListener(this);
        setSwipeBackEnable(false);
    }

    public void setTabsDisplay(int checked) {
        onTabSelected(checked);
        TabWidget.setTabsDisplay(checked);
    }

    @Override
    public void onTabSelected(int checked) {
        mTransaction = mFragmentManager.beginTransaction();
        hideFragment(position);
        showFragment(checked);
    }

    private void showFragment(int checked) {
        StatusBarUtil.setStatusBarColor(this, checked != AppConfig.USER_CENTER_FRAGMENT_INDEX?R.color.white:R.color.red_f8b2bd);
        switch (checked) {
            case AppConfig.INDEX_FRAGMENT_INDEX:
//                if (null == mIndexFragment) {
//                    mIndexFragment = new IndexFragment();
//                    mTransaction.add(R.id.center_layout, mIndexFragment);
//                } else {
//                    mTransaction.show(mIndexFragment);
//                }
//                mIndexFragment.onStartFragment();
                if(fragmentHome == null){
                    fragmentHome = new FragmentHome();
                    mTransaction.add(R.id.center_layout, fragmentHome);
                } else {
                    mTransaction.show(fragmentHome);
                }
                break;
            case AppConfig.JIU_FRAGMENT_INDEX:
                if(fragmentDoubleNine == null){
                    fragmentDoubleNine = new FragmentDoubleNine();
                    mTransaction.add(R.id.center_layout, fragmentDoubleNine);
                } else {
                    mTransaction.show(fragmentDoubleNine);
                }
//                if (null == mJiuFragment) {
//                    mJiuFragment = new JiuFragment();
//                    mTransaction.add(R.id.center_layout, mJiuFragment);
//                } else {
//                    mTransaction.show(mJiuFragment);
//                }
//                mJiuFragment.onStartFragment();
                break;
            case AppConfig.GOOD_BRAND_FRAGMENT_INDEX:
                if(fragmentBrandCategory == null){
                    fragmentBrandCategory = new FragmentBrandCategory();
                    mTransaction.add(R.id.center_layout, fragmentBrandCategory);
                } else {
                    mTransaction.show(fragmentBrandCategory);
                }
//                if (null == mScoreShopFragment) {
//                    mScoreShopFragment = new ScoreShopFragment();
//                    mTransaction.add(R.id.center_layout, mScoreShopFragment);
//                } else {
//                    mTransaction.show(mScoreShopFragment);
//                }
//                mScoreShopFragment.onStartFragment();
                break;
            case AppConfig.CATEGORY_FRAGMENT_INDEX:
                if (null == mCategoryFragment) {
                    mCategoryFragment = new CategoryFragment();
                    mTransaction.add(R.id.center_layout, mCategoryFragment);
                } else {
                    mTransaction.show(mCategoryFragment);
                }
                mCategoryFragment.onStartFragment();
                break;
            case AppConfig.USER_CENTER_FRAGMENT_INDEX:
                if (null == mUserCenterFragment) {
                    mUserCenterFragment = new UserCenterFragment();
                    mTransaction.add(R.id.center_layout, mUserCenterFragment);
                } else {
                    mTransaction.show(mUserCenterFragment);
                }
                mUserCenterFragment.onStartFragment();
                break;
        }
        position = checked;
        mTransaction.commitAllowingStateLoss();
    }

    private void hideFragment(int position) {
        switch (position) {
            case AppConfig.INDEX_FRAGMENT_INDEX:
//                if (null != mIndexFragment) {
//                    mIndexFragment.onStopFragment();
//                    mTransaction.hide(mIndexFragment);
//                }
                if(null != fragmentHome){
                    mTransaction.hide(fragmentHome);
                }
                break;
            case AppConfig.JIU_FRAGMENT_INDEX:
                if(null != fragmentDoubleNine){
                    mTransaction.hide(fragmentDoubleNine);
                }
//                if (null != mJiuFragment) {
//                    mJiuFragment.onStopFragment();
//                    mTransaction.hide(mJiuFragment);
//                }
                break;
            case AppConfig.GOOD_BRAND_FRAGMENT_INDEX:
                if(null != fragmentBrandCategory){
                    mTransaction.hide(fragmentBrandCategory);
                }
//                if (null != mScoreShopFragment) {
//                    mScoreShopFragment.onStopFragment();
//                    mTransaction.hide(mScoreShopFragment);
//                }
                break;
            case AppConfig.CATEGORY_FRAGMENT_INDEX:
                if (null != mCategoryFragment) {
                    mCategoryFragment.onStopFragment();
                    mTransaction.hide(mCategoryFragment);
                }
                break;
            case AppConfig.USER_CENTER_FRAGMENT_INDEX:
                if (null != mUserCenterFragment) {
                    mUserCenterFragment.onStopFragment();
                    mTransaction.hide(mUserCenterFragment);
                }
                break;
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (position) {
            case AppConfig.INDEX_FRAGMENT_INDEX:
                fragmentHome.onActivityResult(requestCode, resultCode, data);
//                mIndexFragment.onActivityResult(requestCode, resultCode, data);
                break;
            case AppConfig.JIU_FRAGMENT_INDEX:
                fragmentDoubleNine.onActivityResult(requestCode, resultCode, data);
//                mJiuFragment.onActivityResult(requestCode, resultCode, data);
                break;
            case AppConfig.GOOD_BRAND_FRAGMENT_INDEX:
                fragmentBrandCategory.onActivityResult(requestCode, resultCode, data);
//                mScoreShopFragment.onActivityResult(requestCode, resultCode, data);
                break;
            case AppConfig.CATEGORY_FRAGMENT_INDEX:
                mCategoryFragment.onActivityResult(requestCode, resultCode, data);
                break;
            case AppConfig.USER_CENTER_FRAGMENT_INDEX:
                mUserCenterFragment.onActivityResult(requestCode, resultCode, data);
                break;
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        //保存选中状态
        outState.putInt("position", position);
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        //恢复选中状态
        position = savedInstanceState.getInt("position");
    }

    @Override
    public void onAttachFragment(Fragment fragment) {
        super.onAttachFragment(fragment);
    }

    /**
     * 监听返回--是否退出程序
     */
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (position != AppConfig.INDEX_FRAGMENT_INDEX) {
            setTabsDisplay(AppConfig.INDEX_FRAGMENT_INDEX);
        } else if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - exitTime) > 1500) {
                // 保存友盟统计数据
                exitTime = System.currentTimeMillis();
                Toast.show(mContext, "再按一次退出「米果特卖」");
            } else {
                this.finish();
                //System.exit(0);
                Statistics.onKillProcess(mContext);
                System.gc();
            }
        }
        return false;
    }

    @Override
    protected void onResume() {
        super.onResume();
        setTabsDisplay(position);
        // 控制更新指示点 false true
        //TabWidget.setIndicateDisplay(AppConfig.TAB_VISIBLE_ID, TabVisible);
    }
}